package swg.shape;

import java.util.ArrayList;

public class BranchOLD implements IswgShape
{
	protected Point start;
	protected Point end;
	protected ArrayList<BranchOLD> brances = new ArrayList<BranchOLD>();
	protected int level = 0;
	protected int lengh = 0;
	protected Point zero;
	protected Color styleColor;
	protected int styleStrike = 0;
	protected boolean caps;
	protected int styleCapsRadius;
	protected Color styleCapsColor;

	public int getLengh()
	{
		return lengh;
	}

	public void setLengh(int lengh)
	{
		this.lengh = lengh;
	}

	public BranchOLD(Point start, Point end)
	{
		this.start = start;
		this.end = end;
		zero = new Point(end.getX() - start.getX(), end.getY() - start.getY());
	}

	public Point getZero()
	{
		return zero;
	}

	public void setZero(Point zero)
	{
		this.zero = zero;
	}

	public Color getStyleColor()
	{
		return styleColor;
	}

	public void setStyleColor(Color styleColor)
	{
		this.styleColor = styleColor;
	}

	public int getStyleStrike()
	{
		return styleStrike;
	}

	public void setStyleStrike(int styleStrike)
	{
		this.styleStrike = styleStrike;
	}

	public boolean isCaps()
	{
		return caps;
	}

	public void setCaps(boolean caps)
	{
		this.caps = caps;
	}

	public int getStyleCapsRadius()
	{
		return styleCapsRadius;
	}

	public void setStyleCapsRadius(int styleCapsRadius)
	{
		this.styleCapsRadius = styleCapsRadius;
	}

	public Color getStyleCapsColor()
	{
		return styleCapsColor;
	}

	public void setStyleCapsColor(Color styleCapsColor)
	{
		this.styleCapsColor = styleCapsColor;
	}

	public Point getStart()
	{
		return start;
	}

	public void setStart(Point start)
	{
		this.start = start;
	}

	public Point getEnd()
	{
		return end;
	}

	public void setEnd(Point end)
	{
		this.end = end;
	}

	public ArrayList<BranchOLD> getBrances()
	{
		return brances;
	}

	public void setBrances(ArrayList<BranchOLD> brances)
	{
		this.brances = brances;
	}

	public int getLevel()
	{
		return level;
	}

	public void setLevel(int level)
	{
		this.level = level;
	}

	public double getPhi()
	{
		return zero.getPhi();
	}

	public double getRho()
	{
		return zero.getRho();
	}

	@Override
	public String toString()
	{
		return "Branch [\n start=" + start + " \n end=" + end + "\n zero=" + zero + "\n level=" + level + "\n]";
	}

	@Override
	public int getR()
	{
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getSWGCode()
	{
		StringBuffer swg = new StringBuffer("\n<line ");
		// <line x1="0" y1="0" x2="200" y2="200"
		// style="stroke:rgb(255,0,0);stroke-width:2"/>
		swg.append(" x1 =\"" + start.getX() + "\"");
		swg.append(" y1 =\"" + start.getY() + "\"");
		swg.append(" x2 =\"" + end.getX() + "\"");
		swg.append(" y2 =\"" + end.getY() + "\"");
		String style="";
		if (styleColor != null)
		{
			style=style+" stroke:" + styleColor.getSWGCode()+"; ";
		}
		if (styleStrike != 0)
		{
			style=style+" stroke-width:" + styleStrike;
		}
		if(style.length()==0){
			style="stroke:rgb(0,0,0) ;  stroke-width:2";
		}

		swg.append(" style=\""+style+"\" />");
		if (caps)
		{
			Circle sc= new Circle(start.getX(), start.getY(), styleCapsRadius,0, "", styleCapsColor.getSWGCode());
			swg.append(sc.getSWGCode());

			Circle se= new Circle(end.getX(), end.getY(), styleCapsRadius,0, "", styleCapsColor.getSWGCode());
			swg.append(se.getSWGCode());
		}
		if(brances.size()>0){
			for(BranchOLD b : brances){
				swg.append(b.getSWGCode());
			}
			
		}
		return swg.toString();
	}

	@Override
	public int getX()
	{
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getY()
	{
		// TODO Auto-generated method stub
		return 0;
	}

	public ArrayList<BranchOLD> getNlevel(int level)
	{
		//double a = 1/(1-1);
		ArrayList<BranchOLD> res = new ArrayList<BranchOLD>();
		if(lengh<level)
			return null;
		if (brances.size()==0)
			return null;
		for (BranchOLD b : brances){
			if (b.getLevel()== level)
				res.add(b);
			if(b.getLevel()<level){
				ArrayList<BranchOLD> subRes = b.getNlevel(level);
				if (subRes!=null && subRes.size()>0)
					res.addAll(subRes);
			}
		}
		
		return res;
	}

}
